/**
 * Created by moko1988
 * 2016年01月05日16:10:34
 */
define([
    'backbone',
    'models/public/resetPassword',
    'text!templets/public/resetPassword.htm'
], function(
    BackBone,
    Model,
    Tpl
) {
    var view = Backbone.View.extend({
        el: '#CONTENT',
        events: {
            'click [role=save]': 'saveFun'
        },
        initialize: function() {
            $(this.el).html(Tpl);
            $(this.el).off();
        },
        saveFun: function() {
            var _this = this,
                form = $('form[role=form]'),
                oldpasswd = form.find('input[name=oldpwd]'),
                newpasswd = form.find('input[name=newpwd]'),
                rnewpasswd = form.find('input[name=rnewpwd]'),
                subBtn = $('[role=save]'),
                opts = [oldpasswd, newpasswd, rnewpasswd],
                isOK = true;
            _.each(opts, function(o) {
                var name = o.attr('name'),
                    tip = $('em[for=' + name + ']').attr('tip-data');
                if ($.trim(o.val()) == '') {
                    $('em[for=' + name + ']').html('！请填写' + tip).css("opacity", '1');
                    $('em[for=' + name + ']').stop(0, 0).animate({
                        "opacity": 0
                    }, 3500);
                    isOK = false;
                } else {
                    if (name == 'newpwd' && (o.val().length < 6 || o.val().length > 20)) {
                        $('em[for=' + name + ']').html('！' + tip + '长度为6-20个字符').css("opacity", '1');
                        $('em[for=' + name + ']').stop(0, 0).animate({
                            "opacity": 0
                        }, 3500);
                        isOK = false;
                    }
                    if (name == 'rnewpwd' && o.val() != newpasswd.val()) {
                        $('em[for=' + name + ']').html('！两次密码输入不一致').css("opacity", '1');
                        $('em[for=' + name + ']').stop(0, 0).animate({
                            "opacity": 0
                        }, 3500);
                        isOK = false;
                    }
                }
            })
            if (isOK) {
                subBtn.html('正在提交..').prop('disabled', true);
                Model.setPassword({
                    data: form.serialize(),
                    success: function() {
                        subBtn.html('提 交').prop('disabled', false);
                        jAlert('设置成功！', function(){
                            window.location.replace(adminPath+ '/index/logout');
                        });
                    },
                    error: function(err) {
                        jAlert(err, function() {
                            subBtn.html('提 交').prop('disabled', false);
                        });
                    }
                })
            }
        }
    })
    return view;
})